<!DOCTYPE html>
<html>
<head>
    

    

    
<!-- Baidu Tongji -->
<script>var _hmt = _hmt || []</script>
<script async src="//hm.baidu.com/hm.js?a7c05ce530152d9866930ef4850ee566"></script>
<!-- End Baidu Tongji -->




    <meta charset="utf-8">
    
    
    
    <title>Redis基础入门 | 神奇的鸭鸭の码农库 | 新知识要不断的总结记录成笔记，要多写，多画，能够清晰透彻的将知识讲给别人听，才是达到理解的层次。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#765959">
    
    
    <meta name="keywords" content="缓存">
    <meta name="description" content="Redis介绍什么是NoSql为了解决高并发，高科扩展，高可用，大数据存储问题而产生的数据库解决方案，就是NoSql数据库。NoSql泛指非关系型的数据库，NoSql即Not-Only SQL，它可以作为关系型数据库的良好补充。 NoSql数据库分类键值(Key-Value)存储数据库：  相关产品： Tokyo Cabinet/Tyrant、Redis、Voldemort、Berkeley DB">
<meta name="keywords" content="缓存">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis基础入门">
<meta property="og:url" content="http://magic_duck.oschina.io/2017/09/06/redis4.4.0_primary_01/index.html">
<meta property="og:site_name" content="神奇的鸭鸭の码农库">
<meta property="og:description" content="Redis介绍什么是NoSql为了解决高并发，高科扩展，高可用，大数据存储问题而产生的数据库解决方案，就是NoSql数据库。NoSql泛指非关系型的数据库，NoSql即Not-Only SQL，它可以作为关系型数据库的良好补充。 NoSql数据库分类键值(Key-Value)存储数据库：  相关产品： Tokyo Cabinet/Tyrant、Redis、Voldemort、Berkeley DB">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/Redis%20Desktop%20Manager.png">
<meta property="og:updated_time" content="2017-12-05T09:59:03.650Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Redis基础入门">
<meta name="twitter:description" content="Redis介绍什么是NoSql为了解决高并发，高科扩展，高可用，大数据存储问题而产生的数据库解决方案，就是NoSql数据库。NoSql泛指非关系型的数据库，NoSql即Not-Only SQL，它可以作为关系型数据库的良好补充。 NoSql数据库分类键值(Key-Value)存储数据库：  相关产品： Tokyo Cabinet/Tyrant、Redis、Voldemort、Berkeley DB">
<meta name="twitter:image" content="http://om6odecqr.bkt.clouddn.com/Redis%20Desktop%20Manager.png">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.4.3">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">神奇的鸭鸭</h5>
          <a href="mailto:702038338@qq.com" title="702038338@qq.com" class="mail">702038338@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://git.oschina.net/magic_duck" target="_blank" >
                <i class="icon icon-lg icon-gg-circle"></i>
                码云
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.zhihu.com/people/shen-qi-de-ya-ya" target="_blank" >
                <i class="icon icon-lg icon-twitter"></i>
                知乎
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/404.html"  >
                <i class="icon icon-lg icon-link"></i>
                404页面测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Redis基础入门</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Redis基础入门</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-09-05T17:00:00.000Z" itemprop="datePublished" class="page-time">
  2017-09-06
</time>


            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Redis介绍"><span class="post-toc-number">1.</span> <span class="post-toc-text">Redis介绍</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#什么是NoSql"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">什么是NoSql</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#NoSql数据库分类"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">NoSql数据库分类</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#什么是Redis"><span class="post-toc-number">1.3.</span> <span class="post-toc-text">什么是Redis</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Redis历史发展"><span class="post-toc-number">1.4.</span> <span class="post-toc-text">Redis历史发展</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Redis的应用场景"><span class="post-toc-number">1.5.</span> <span class="post-toc-text">Redis的应用场景</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Redis安装配置"><span class="post-toc-number">2.</span> <span class="post-toc-text">Redis安装配置</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Redis启动"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">Redis启动</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Redis客户端"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">Redis客户端</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#java客户端Jedis"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">java客户端Jedis</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-redis4.4.0_primary_01"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Redis基础入门</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年09月06日 1:00" datetime="2017-09-05T17:00:00.000Z"  itemprop="datePublished">2017-09-06</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="Redis介绍"><a href="#Redis介绍" class="headerlink" title="Redis介绍"></a>Redis介绍</h2><h3 id="什么是NoSql"><a href="#什么是NoSql" class="headerlink" title="什么是NoSql"></a>什么是NoSql</h3><p>为了解决高并发，高科扩展，高可用，大数据存储问题而产生的数据库解决方案，就是NoSql数据库。NoSql泛指非关系型的数据库，NoSql即Not-Only SQL，它可以作为关系型数据库的良好补充。</p>
<h3 id="NoSql数据库分类"><a href="#NoSql数据库分类" class="headerlink" title="NoSql数据库分类"></a>NoSql数据库分类</h3><p><strong>键值(Key-Value)存储数据库：</strong></p>
<ul>
<li>相关产品： Tokyo Cabinet/Tyrant、Redis、Voldemort、Berkeley DB</li>
<li>典型应用： 内容缓存，主要用于处理大量数据的高访问负载。 </li>
<li>数据模型： 一系列键值对</li>
<li>优势： 快速查询</li>
<li>劣势： 存储的数据缺少结构化</li>
</ul>
<p><strong>列存储数据库：</strong></p>
<ul>
<li>相关产品：Cassandra, HBase, Riak</li>
<li>典型应用：分布式的文件系统</li>
<li>数据模型：以列簇式存储，将同一列数据存在一起</li>
<li>优势：查找速度快，可扩展性强，更容易进行分布式扩展</li>
<li>劣势：功能相对局限</li>
</ul>
<p><strong>文档型数据库：</strong></p>
<ul>
<li>相关产品：CouchDB、MongoDB</li>
<li>典型应用：Web应用（与Key-Value类似，Value是结构化的）</li>
<li>数据模型： 一系列键值对</li>
<li>优势：数据结构要求不严格</li>
<li>劣势： 查询性能不高，而且缺乏统一的查询语法</li>
</ul>
<p><strong>图形(Graph)数据库：</strong></p>
<ul>
<li>相关数据库：Neo4J、InfoGrid、Infinite Graph</li>
<li>典型应用：社交网络</li>
<li>数据模型：图结构</li>
<li>优势：利用图结构相关算法。</li>
<li>劣势：需要对整个图做计算才能得出结果，不容易做分布式的集群方案。</li>
</ul>
<h3 id="什么是Redis"><a href="#什么是Redis" class="headerlink" title="什么是Redis"></a>什么是Redis</h3><p>Redis是用C语言开发的一个开源的<strong>高性能键值对(key-value)数据库</strong>。它通过提供<strong>多种键值数据类型</strong>来适应不同场景下的存储需求，目前为止Redis支持的键值数据类型如下：<strong>字符串类型，散列类型，列表类型，集合类型，有序集合类型</strong>。</p>
<h3 id="Redis历史发展"><a href="#Redis历史发展" class="headerlink" title="Redis历史发展"></a>Redis历史发展</h3><p>2008年，意大利的一家创业公司Merzia推出了一款基于MySQL的网站实时统计系统LLOOGG，然而没过多久该公司的创始人 Salvatore Sanfilippo便 对MySQL的性能感到失望，于是他决定亲自为LLOOGG量身定做一个数据库，并于2009年开发完成，这个数据库就是Redis。 不过Salvatore Sanfilippo并不满足只将Redis用于LLOOGG这一款产品，而是希望更多的人使用它，于是在同一年Salvatore Sanfilippo将Redis开源发布，并开始和Redis的另一名主要的代码贡献者Pieter Noordhuis一起继续着Redis的开发，直到今天。</p>
<p>Salvatore Sanfilippo自己也没有想到，短短的几年时间，Redis就拥有了庞大的用户群体。Hacker News在2012年发布了一份数据库的使用情况调查，结果显示有近12%的公司在使用Redis。国内如新浪微博、街旁网、知乎网，国外如GitHub、Stack Overflow、Flickr等都是Redis的用户。</p>
<p>VMware公司从2010年开始赞助Redis的开发， Salvatore Sanfilippo和Pieter Noordhuis也分别在3月和5月加入VMware，全职开发Redis。</p>
<h3 id="Redis的应用场景"><a href="#Redis的应用场景" class="headerlink" title="Redis的应用场景"></a>Redis的应用场景</h3><ul>
<li>缓存(数据查询，短连接，新闻内容，商品内容等等)。<strong>Redis主要用于内容缓存，它主要用于处理大量数据的高访问负载。</strong></li>
<li>分布式集群架构中的session分离</li>
<li>聊天室的在线好友列表</li>
<li>任务队列(秒杀，抢购，12306等等)</li>
<li>应用排行榜</li>
<li>网站访问统计</li>
<li>数据过期处理(可以精确到毫秒)</li>
</ul>
<hr>
<h2 id="Redis安装配置"><a href="#Redis安装配置" class="headerlink" title="Redis安装配置"></a>Redis安装配置</h2><p>官网下载地址：<a href="http://www.redis.cn/，Redis是C语言开发，建议在linux上运行，本文使用腾讯云乞丐机CentOS" target="_blank" rel="external">http://www.redis.cn/，Redis是C语言开发，建议在linux上运行，本文使用腾讯云乞丐机CentOS</a> 7.0 64位作为安装环境，使用Xshell进行远程连接。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#</span> 在Linux下安装gcc环境（该步骤可以省略，CentOS中默认自带C环境）</div><div class="line">yum install gcc-c++</div><div class="line"><span class="meta">#</span> 使用Xshell，我们可以将要上传的文件直接拖拽到linux上，但是必须先安装lrzsz这个软件包：</div><div class="line">yum -y install lrzsz</div><div class="line"><span class="meta">#</span> 将下载当前稳定版的Redis4.0.1源码包上传到Linux服务器中</div><div class="line"><span class="meta">#</span> 解压redis源码包</div><div class="line">tar -zxf redis-4.0.1.tar.gz</div><div class="line"><span class="meta">#</span> 编译redis源码</div><div class="line">cd redis-4.0.1</div><div class="line">make</div><div class="line"><span class="meta">#</span> 安装redis</div><div class="line">make install PREFIX=/usr/local/redis-4.0.1</div></pre></td></tr></table></figure>
<h3 id="Redis启动"><a href="#Redis启动" class="headerlink" title="Redis启动"></a>Redis启动</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#</span> 前端启动</div><div class="line"><span class="meta">#</span> 直接运行bin/redis-server将以前端模式启动</div><div class="line"><span class="meta">#</span> 缺点：ssh命令窗口关闭则redis-server程序结束，不推荐使用此方法</div><div class="line">cd /usr/local/redis-4.0.1/bin/</div><div class="line">./redis-server</div><div class="line"><span class="meta">#</span> 后端启动</div><div class="line"><span class="meta">#</span> 将redis源码包中的redis.conf配置文件复制到/usr/local/redis/bin/下</div><div class="line">cp redis.conf /usr/local/redis-4.0.1/bin/</div><div class="line"><span class="meta">#</span> 修改redis.conf，启用后台运行需要将daemonize由no改为yes</div><div class="line"><span class="meta">#</span> 修改redis.conf，开放公网访问，需要将bind的IP改为 0.0.0.0，不改的话只能本机访问redis，外机是无法访问的</div><div class="line">vim redis.conf</div><div class="line"><span class="meta">#</span> daemonize yes</div><div class="line"><span class="meta">#</span> bind 0.0.0.0</div><div class="line"><span class="meta">#</span> 执行后台启动</div><div class="line">./redis-server redis.conf</div><div class="line"><span class="meta">#</span> 关闭后端启动(不推荐)</div><div class="line">kill 5528</div><div class="line"><span class="meta">#</span> 关闭后端启动(推荐)</div><div class="line">./redis-cli shutdown</div></pre></td></tr></table></figure>
<h3 id="Redis客户端"><a href="#Redis客户端" class="headerlink" title="Redis客户端"></a>Redis客户端</h3><p><strong>Redis自带的客户端：</strong><br><figure class="highlight shell"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#</span> 指定主机和端口 -h redis服务器的ip地址 -p 实例的端口号</div><div class="line">./redis-cli -h 127.0.0.1 -p 6379</div><div class="line"><span class="meta">#</span> 如果不指定也可以默认 127.0.0.1 6379</div><div class="line">./redis-cli</div></pre></td></tr></table></figure></p>
<p><strong>图形界面客户端：</strong><br>默认一共是16个数据库，每个数据库之间是相互隔离。数据库的数量是在redis.conf中配置的。切换数据库使用命令：select 数据库编号<br><img src="http://om6odecqr.bkt.clouddn.com/Redis%20Desktop%20Manager.png" alt="Redis Desktop Manager"></p>
<h3 id="java客户端Jedis"><a href="#java客户端Jedis" class="headerlink" title="java客户端Jedis"></a>java客户端Jedis</h3><p>Redis不仅是使用命令来操作，现在基本上主流的语言都有客户端支持，比如java、C、C#、C++、php、Node.js、Go等。在官方网站里列一些Java的客户端，有Jedis、Redisson、Jredis、JDBC-Redis、等其中官方推荐使用Jedis和Redisson。</p>
<p>添加jar包，我现在一般先下载到maven repository中，需要用到：commons-pool2-2.4.2.jar，jedis-2.9.0.jar</p>
<p><strong>单实例连接：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">jedisClient</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="comment">// 创建一个Jedis连接</span></div><div class="line">    Jedis jedis = <span class="keyword">new</span> Jedis(<span class="string">"118.89.59.106"</span>, <span class="number">6379</span>);</div><div class="line">    <span class="comment">// 执行redis命令</span></div><div class="line">    jedis.set(<span class="string">"mytest"</span>, <span class="string">"hello world,this is jedis client!"</span>);</div><div class="line">    <span class="comment">// 从redis中取值</span></div><div class="line">    String result = jedis.get(<span class="string">"mytest"</span>);</div><div class="line">    System.out.println(result);</div><div class="line">    <span class="comment">// 关闭连接</span></div><div class="line">    jedis.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p><strong>连接池连接：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">jedisPool</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="comment">// 创建一个连接池对象</span></div><div class="line">    JedisPool jedisPool = <span class="keyword">new</span> JedisPool(<span class="string">"118.89.59.106"</span>, <span class="number">6379</span>);</div><div class="line">    <span class="comment">// 从连接池中获得Jedis连接</span></div><div class="line">    Jedis jedis = jedisPool.getResource();</div><div class="line">    String result = jedis.get(<span class="string">"mytest"</span>);</div><div class="line">    System.out.println(result);</div><div class="line">    <span class="comment">// 关闭连接</span></div><div class="line">    jedis.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p><strong>使用spring整合jedisPool，首先添加spring的jar包，然后配置spring配置文件applicationContext.xml：</strong><br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</div><div class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></div><div class="line"><span class="tag">    <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="attr">xmlns:mvc</span>=<span class="string">"http://www.springframework.org/schema/mvc"</span></span></div><div class="line"><span class="tag">    <span class="attr">xmlns:context</span>=<span class="string">"http://www.springframework.org/schema/context"</span></span></div><div class="line"><span class="tag">    <span class="attr">xmlns:aop</span>=<span class="string">"http://www.springframework.org/schema/aop"</span> <span class="attr">xmlns:tx</span>=<span class="string">"http://www.springframework.org/schema/tx"</span></span></div><div class="line"><span class="tag">    <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans </span></span></div><div class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd </span></span></div><div class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/mvc </span></span></div><div class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd </span></span></div><div class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/context </span></span></div><div class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/context/spring-context-4.3.xsd </span></span></div><div class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/aop </span></span></div><div class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd </span></span></div><div class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/tx </span></span></div><div class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd "</span>&gt;</span></div><div class="line"></div><div class="line">    <span class="comment">&lt;!-- 连接池配置 --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">"jedisPoolConfig"</span> <span class="attr">class</span>=<span class="string">"redis.clients.jedis.JedisPoolConfig"</span>&gt;</span></div><div class="line">        <span class="comment">&lt;!-- 最大连接数 --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"maxTotal"</span> <span class="attr">value</span>=<span class="string">"30"</span> /&gt;</span></div><div class="line">        <span class="comment">&lt;!-- 最大空闲连接数 --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"maxIdle"</span> <span class="attr">value</span>=<span class="string">"10"</span> /&gt;</span></div><div class="line">        <span class="comment">&lt;!-- 每次释放连接的最大数目 --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"numTestsPerEvictionRun"</span> <span class="attr">value</span>=<span class="string">"1024"</span> /&gt;</span></div><div class="line">        <span class="comment">&lt;!-- 释放连接的扫描间隔（毫秒） --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"timeBetweenEvictionRunsMillis"</span> <span class="attr">value</span>=<span class="string">"30000"</span> /&gt;</span></div><div class="line">        <span class="comment">&lt;!-- 连接最小空闲时间 --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"minEvictableIdleTimeMillis"</span> <span class="attr">value</span>=<span class="string">"1800000"</span> /&gt;</span></div><div class="line">        <span class="comment">&lt;!-- 连接空闲多久后释放, 当空闲时间&gt;该值 且 空闲连接&gt;最大空闲连接数 时直接释放 --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"softMinEvictableIdleTimeMillis"</span> <span class="attr">value</span>=<span class="string">"10000"</span> /&gt;</span></div><div class="line">        <span class="comment">&lt;!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"maxWaitMillis"</span> <span class="attr">value</span>=<span class="string">"1500"</span> /&gt;</span></div><div class="line">        <span class="comment">&lt;!-- 在获取连接的时候检查有效性, 默认false --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"testOnBorrow"</span> <span class="attr">value</span>=<span class="string">"false"</span> /&gt;</span></div><div class="line">        <span class="comment">&lt;!-- 在空闲时检查有效性, 默认false --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"testWhileIdle"</span> <span class="attr">value</span>=<span class="string">"true"</span> /&gt;</span></div><div class="line">        <span class="comment">&lt;!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"blockWhenExhausted"</span> <span class="attr">value</span>=<span class="string">"false"</span> /&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></div><div class="line"></div><div class="line">    <span class="comment">&lt;!-- redis单机 通过连接池 --&gt;</span></div><div class="line">    <span class="comment">&lt;!-- destroy-method="close" 当数据库连接不使用时，自动将该连接放入连接池中，方便下次调用 --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"jedisPool"</span> <span class="attr">class</span>=<span class="string">"redis.clients.jedis.JedisPool"</span></span></div><div class="line"><span class="tag">        <span class="attr">destroy-method</span>=<span class="string">"close"</span>&gt;</span></div><div class="line">        <span class="comment">&lt;!-- public JedisPool(final GenericObjectPoolConfig poolConfig, final </span></div><div class="line"><span class="comment">            String host, final int port) --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">constructor-arg</span> <span class="attr">name</span>=<span class="string">"poolConfig"</span> <span class="attr">ref</span>=<span class="string">"jedisPoolConfig"</span>&gt;</span><span class="tag">&lt;/<span class="name">constructor-arg</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">constructor-arg</span> <span class="attr">name</span>=<span class="string">"host"</span> <span class="attr">value</span>=<span class="string">"118.89.59.106"</span>&gt;</span><span class="tag">&lt;/<span class="name">constructor-arg</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">constructor-arg</span> <span class="attr">name</span>=<span class="string">"port"</span> <span class="attr">value</span>=<span class="string">"6379"</span>&gt;</span><span class="tag">&lt;/<span class="name">constructor-arg</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p><strong>测试代码：</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@RunWith</span>(SpringJUnit4ClassRunner.class)</div><div class="line"><span class="meta">@ContextConfiguration</span>(<span class="string">"classpath:applicationContext.xml"</span>)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">testJedisPool</span> </span>&#123;</div><div class="line">    </div><div class="line">    <span class="meta">@Autowired</span></div><div class="line">    JedisPool pool;</div><div class="line">    </div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testJedisPool</span><span class="params">()</span> </span>&#123;</div><div class="line">        Jedis jedis = pool.getResource();</div><div class="line">        String result = jedis.get(<span class="string">"mytest"</span>);</div><div class="line">        System.out.println(result);</div><div class="line">        jedis.close();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-12-05T09:59:03.650Z" itemprop="dateUpdated">2017年12月5日 17:59</time>
</span><br>


        如要转载请注明出处：<a href="/2017/09/06/redis4.4.0_primary_01/" target="_blank" rel="external">http://magic_duck.oschina.io/2017/09/06/redis4.4.0_primary_01/</a>
    </div>
    <footer>
        <a href="http://magic_duck.oschina.io">
            <img src="/img/avatar.jpg" alt="神奇的鸭鸭">
            神奇的鸭鸭
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/缓存/">缓存</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/09/06/redis4.4.0_primary_01/&title=《Redis基础入门》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/09/06/redis4.4.0_primary_01/&title=《Redis基础入门》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/09/06/redis4.4.0_primary_01/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Redis基础入门》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/09/06/redis4.4.0_primary_01/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/09/06/redis4.4.0_primary_01/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/09/06/redis4.4.0_primary_02/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Redis数据类型和命令</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/09/04/Solr6.6.0_primary_01/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">全文检索技术Solr6.6.0</h4>
      </a>
    </div>
  
</nav>



    







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        我只要一角钱~ ~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.png" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.jpg" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://git.oschina.net/z77z" target="_blank">邹海清</a></span>
            <span>神奇的鸭鸭の码农库 &copy; 2017</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/09/06/redis4.4.0_primary_01/&title=《Redis基础入门》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/09/06/redis4.4.0_primary_01/&title=《Redis基础入门》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/09/06/redis4.4.0_primary_01/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Redis基础入门》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/09/06/redis4.4.0_primary_01/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/09/06/redis4.4.0_primary_01/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://magic_duck.oschina.io/2017/09/06/redis4.4.0_primary_01/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



lazyScripts.push('//s95.cnzz.com/z_stat.php?id=1261081671&web_id=1261081671')

</script>

<script src="/js/main.min.js?v=1.4.3"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.4.3" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
